<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use yii\widgets\Pjax;
use yii\grid\GridView;
use kartik\export\ExportMenu;

/* @var $this yii\web\View */
/* @var $searchModel app\models\MaterialSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '资料';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="material-index">

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class="col-md-12" style="margin-bottom: 15px;">      
        <?php
        $form = ActiveForm::begin([
                    'action' => ['index'],
                    'method' => 'get',
                    'options' => ['class' => 'input-group'],
                    'fieldConfig' => [
                        'template' => '<div class="col-lg-12">{input}</div>',
                    ]
                ])
        ?>

        <div class="col-md-5">
            <label style="float: left;margin-top: 5px;">材料名称</label>
            <div style="overflow: hidden;">
                <?php
//                $data = app\models\Material::find()->all();
//                echo $form->field($searchModel, 'materialname')->widget(Select2::classname(), [
//                    'data' => ArrayHelper::map($data, 'materialname', 'materialname'),
//                    'options' => ['placeholder' => '请输入内容'],
//                    'pluginOptions' => [
//                        'allowClear' => true
//                    ],
//                ]);
                ?>  
                  <?php
                $datas = app\models\Material::find()->all();
                if ($datas != null) {
                    foreach ($datas as $item) {
                        $source[] = $item->materialname;
                    }
                } else {
                    $source[] = array();
                }
                echo yii\jui\AutoComplete::widget([
                    'model' => $searchModel,
                    'attribute' => 'materialname',
                    'options'=>['style'=>'margin-left:10px'],
                    'clientOptions' => [
                        'source' => $source,
                    ],
                ]);
                ?>
            </div>
        </div>
        <div class="col-md-5">
            <label style="float: left;margin-top: 5px;">材料编号</label>
            <div style="overflow: hidden;">
                <?php
//                $data = app\models\Material::find()->all();
//                echo $form->field($searchModel, 'materialno')->widget(Select2::classname(), [
//                    'data' => ArrayHelper::map($data, 'materialno', 'materialno'),
//                    'options' => ['placeholder' => '请输入内容'],
//                    'pluginOptions' => [
//                        'allowClear' => true
//                    ],
//                ]);
                ?> 
                  <?php
                if ($datas != null) {
                    foreach ($datas as $item) {
                        $source[] = $item->materialno;
                    }
                } else {
                    $source[] = array();
                }
                echo yii\jui\AutoComplete::widget([
                    'model' => $searchModel,
                    'attribute' => 'materialno',
                    'options'=>['style'=>'margin-left:10px'],
                    'clientOptions' => [
                        'source' => $source,
                    ],
                ]);
                ?>
            </div>
        </div>

        

        <div class="col-md-2 text-center">
            <?= Html::submitButton(' 搜索', ['class' =>  'btn btn-primary glyphicon glyphicon-search']) ?> 
        </div>

    </div>
    
 <div class="col-md-12 text-right" style="margin: 10px 0 15px 15px;float: right;padding-right: 0">
     
       <?= Html::a('新增', ['create'], ['class' => 'btn btn-primary']) ?> 
        <button class="btn btn-success glyphicon glyphicon-print" onclick="printme()"> 打印</button>
         <span>&nbsp;&nbsp;</span>
         
               <!--为什么要多加一个form才可以导出呢？？？-->
        <?php
        $form = ActiveForm::begin([
                    'action' => ['index'],
                    'options' => ['enctype' => 'multipart/form-data', 'id' => 'form2', 'name' => 'form2']
                ])
        ?>
        <?php ActiveForm::end(); ?>
         
     <?Php
        echo ExportMenu::widget([
            'dataProvider' => $dataProvider,
            'exportConfig' => [
                  ExportMenu::FORMAT_TEXT => false,
                  ExportMenu::FORMAT_PDF => false,
                  ExportMenu::FORMAT_CSV => false,
                  ExportMenu::FORMAT_EXCEL_X => false,
                  ExportMenu::FORMAT_HTML => false
              ],
             'dropdownOptions' => [
                'label' => '导出Excel',
                'class' => 'btn btn-default',
                'icon' => '',
            ],
            'showColumnSelector' => false,
            'columns' =>[
//                ['class' => 'yii\grid\SerialColumn'],
//            'id',
            'materialno',
            'materialtype',
            'materialname',
            'summary',
             'updatetime',
             'isfinish',
             'addman',
             'addmanid',
             'addtime',

            ]
            ]);
    ?>
    </div>
    
    <?php ActiveForm::end(); ?>    
    <?php Pjax::begin(); ?>
    <?= GridView::widget([
         'id' => 'tableContainer',
        'dataProvider' => $dataProvider,
        'columns' => [
           // ['class' => 'yii\grid\SerialColumn'],

            //'id',
//                  'materialname',
            [
                'attribute' => 'materialname',
                'label' => '材料名称',
                'format' => 'raw',               
                'value'=>function ($model) {
                      return Html::a($model->materialname,['material/view','id'=>$model->id]);
                      },
                'contentOptions' => ['style' => ''],
                'headerOptions' => ['style' => ''],
            ],
            
            'materialno',
            'materialtype',
      
            'summary',
            'updatetime',
            // 'isfinish',
            // 'addman',
            // 'addmanid',
            // 'addtime',

            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{update} {delete}',
                'buttons' => [
                    //update button
                    'update' => function ($url) {
                        return Html::a('<span class="fa fa-search"></span>编辑', $url, [
                                    'title' => Yii::t('app', 'Update'),
                                        // 'class' => 'btn btn-primary btn-md',
                        ]);
                    },
                            //update button
                            'delete' => function ($url) {
                        return Html::a('<span class="fa fa-search"></span>删除', $url, [
                                    'title' => Yii::t('app', 'Delete'),
                                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                                    'data-method' => 'post',
                                    'data-pjax' => '0',
                                        //  'class' => 'btn btn-primary btn-md',
                        ]);
                    },
                        ],
                    ],
        ],
    ]); ?>
<?php Pjax::end(); ?>
</div>
<script>

            var global_Html = "";
            function printme() {
                global_Html = document.body.innerHTML;
                document.body.innerHTML = $('#tableContainer').html();
                $('.btn').hide();
                $('.pagination').hide();
                $('a').each(function(){
                    $(this).replaceWith($(this).html());
                });
                window.print();
                window.setTimeout(function () {
                    document.body.innerHTML = global_Html;
                }, 300);
            }

        </script>